package com.rjj.wlqq.wxpay.wxpay.controller;

import com.rjj.wlqq.wxpay.wxpay.WxPayKit;
import com.rjj.wlqq.wxpay.wxpay.entity.Wx;
import com.rjj.wlqq.wxpay.wxpay.entity.WxOrder;
import com.rjj.wlqq.wxpay.wxsdk.WXPayConstants;
import com.rjj.wlqq.wxpay.wxsdk.WXPayUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author r
 * @date 2021/1/20 19:36
 * <p>
 * 下单后返回前段，前段执行微信提供的js进行支付
 * <p>
 * 每次返回【支付签名失败问题】
 * <p>
 * 解决：
 * 1，签名方式使用微信sdk提供的加密方式
 * 2， result.put("nonceStr",unifiedorder.get("nonce_str"));  随机字符串需要使用微信下单后反给我们的字符串 自己生成的无效
 * 3，微信单号          result.put("package","prepay_id="+unifiedorder.get("prepay_id"));  需要按照文档拼接
 * 4，加密方式需要四个参数就可以
 * 【
 * result.put("appId",unifiedorder.get("appid"));
 * result.put("timeStamp",WXPayUtil.getCurrentTimestamp()+"");
 * result.put("signType",WXPayConstants.SignType.HMACSHA256.toString());
 * result.put("package","prepay_id="+unifiedorder.get("prepay_id"));
 * result.put("nonceStr",unifiedorder.get("nonce_str"));
 * 】
 * 5，加密方式  final WXPay wxPay = new WXPay(wxPayConfig); 默认加密方式是 HMACSHA256
 * 6，下单的加密方式和支付的加密方式需要保持一直
 */
@Controller
public class PayController
{

    public static String signStr(Map<String, String> data, String key, String signType)
    {
        try
        {
            if (WXPayConstants.SignType.MD5.equals(signType))
            {
                return WXPayUtil.generateSignature(data, key, WXPayConstants.SignType.MD5);
            } else
            {
                return WXPayUtil.generateSignature(data, key, WXPayConstants.SignType.HMACSHA256);
            }
        } catch (Exception e)
        {
            return null;
        }
    }

    @RequestMapping("/pay")
    public String test(HttpServletRequest request, Map<String, String> result) throws Exception
    {
        final WxOrder build = WxOrder.Builder()
                .setOut_trade_no("p123213123133332")
                .setTrade_type(WXPayConstants.TradeType.JSAPI.toString())
                .setOpenid("ocd6D1hlq-Goo9DOZcXhzinAbz68")
                .setTotal_fee("0.01")
                .setBody("测试商品")
                .setSpbill_create_ip("127.0.0.1")
                .setNotify_url("http://reg.dev.tcm166.cn/pay/wx/notify")
                .build();
        WxPayKit wxPayKit = new WxPayKit()
        {
            @Override
            public Wx initWx()
            {
                return Wx.builder()
                        .appId("wx6aa1dbb87873f9cf")
                        .mchId("1605435869")
                        .key("vG8xK6oH4fP3yN0sY1oH9HH2PK4tJ8xK")
                        .pathFile("/Users/renjianjun/study/ideaWorkSpace/gyzx-master/pay/fengqing_wx_apiclient_cert.p12")
                        .build();
            }
        };
        final Map<String, String> unifiedorder = wxPayKit.unifiedorder(build);


//        Map<String, String> payInfo = new HashMap<>(16);
//        payInfo.put("appId", unifiedorder.get("appid"));
//        payInfo.put("timeStamp", WXPayUtil.getCurrentTimestamp()+"");
//        payInfo.put("nonceStr", unifiedorder.get("nonce_str"));
//        payInfo.put("signType", WXPayConstants.SignType.HMACSHA256.toString());
//        payInfo.put("package", "prepay_id=" + unifiedorder.get("prepay_id"));
//        String paySign = signStr(payInfo, "vG8xK6oH4fP3yN0sY1oH9HH2PK4tJ8xK", WXPayConstants.SignType.HMACSHA256.toString());

        result.put("appId", unifiedorder.get("appid"));
        result.put("timeStamp", WXPayUtil.getCurrentTimestamp() + "");
        result.put("signType", WXPayConstants.SignType.HMACSHA256.toString());
        result.put("package", "prepay_id=" + unifiedorder.get("prepay_id"));
        result.put("nonceStr", unifiedorder.get("nonce_str"));
        final String sign = WXPayUtil.generateSignature(result, "vG8xK6oH4fP3yN0sY1oH9HH2PK4tJ8xK", WXPayConstants.SignType.HMACSHA256);
        result.put("paySign", sign);
        request.setAttribute("map", result);
        return "wxpay/pay";
    }

}
